import { defineStore } from 'pinia'

export const globalVariable = defineStore(
  'globalVariable',
  () => {
    const state = reactive({
      categories: [
        {
          id: 'work',
          name: '工作',
          color: '#165DFF',
          count: 8
        },
        {
          id: 'study',
          name: '学习',
          color: '#36CFC9',
          count: 5
        },
        {
          id: 'personal',
          name: '个人',
          color: '#722ED1',
          count: 3
        },
        {
          id: 'health',
          name: '健康',
          color: '#F5222D',
          count: 2
        }
      ],
    })
    
    // 添加新分类
    const addCategory = (category) => {
      state.categories.push({
        id: Date.now().toString(),
        name: category.name,
        color: category.color,
        count: 0
      })
    }

    // 根据ID增加分类任务数
    const incrementCategoryCount = (categoryId) => {
      const category = state.categories.find(c => c.id === categoryId)
      if (category) {
        category.count++
      }
    }

    // 根据ID获取分类
    const getCategoryById = (categoryId) => {
      return state.categories.find(c => c.id === categoryId)
    }

    // 根据名称获取分类
    const getCategoryByName = (categoryName) => {
      return state.categories.find(c => c.name === categoryName)
    }
    const methods = {
      addCategory,
      incrementCategoryCount,
      getCategoryById,
      getCategoryByName
    }

    return { ...toRefs(state), ...methods }
  },
  {
    persist: {
      enabled: true,
      strategies: [
        {
          storage: localStorage,
          paths: ['categories']
        },
      ],
    },
  },
)
